﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Threading;
using Sparrow.Extensions;
using Sparrow.IoC;

namespace Sparrow.Globalization.Providers
{
    [OrderedExport(typeof(IElementProvider),1000)]
    public class XmlElementProvider:IElementProvider
    {
        //public XmlElementProvider()
        //{
        //    this.Timer = new Timer(this.Flush, null, 0, 5 * 60 * 1000)
        //    {
                
        //    };
        //}

        //Timer Timer
        //{
        //    get;
        //    set;
        //}

        //XDocument _Document;
        //XDocument Document
        //{
        //    get
        //    {
        //        if (this._Document == null)
        //        {
        //            using (var reader = this.GetXmlReader())
        //            {
        //                this._Document = XDocument.Load(reader);
        //            }
        //        }

        //        return this._Document;
        //    }
        //}

        //string ElementsFile
        //{
        //    get
        //    {
        //        return Environments.ApplicationPath + "\\I18N\\Elements.xml";
        //    }
        //}

        //XmlReader GetXmlReader()
        //{


        //    if (File.Exists(this.ElementsFile))
        //    {
        //        return XmlReader.Create(this.ElementsFile);
        //    }
        //    else
        //    {
        //        return XmlReader.Create(new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\" ?><Elements version=\"1.0\"></Elements>"));
        //    }
        //}

        //List<Element> _InnerElements;
        //List<Element> InnerElements
        //{
        //    get
        //    {
        //        if (this._InnerElements == null)
        //        {
        //            this._InnerElements = this.Document.Element("Elements").Elements("Element")
        //                .Select(i => new Element
        //                {
        //                    Name = i.Attribute("Name").GetValue(),
        //                    Category = i.Attribute("Category").GetValue(),
        //                    Culture = i.Attribute("Culture").GetValue(),
        //                    Value = i.Value
        //                })
        //                .ToList();

        //            this.LatestFlushDate = this.LatestUpdateDate = DateTime.Now;
        //        }

        //        return this._InnerElements;
        //    }
            
        //}

        //#region IElementProvider Members

        //public IQueryable<Element>  Elements
        //{
        //    get 
        //    {
        //        return this.InnerElements.AsQueryable();               
        //    }
        //}

        //public bool Add(Element element)
        //{
           
        //    this.InnerElements.Add(element);
        //    this.LatestUpdateDate = DateTime.Now;

        //    return true;

        //}

        //public bool Remove(Element element)
        //{
        //    this.LatestUpdateDate = DateTime.Now;
        //    return this.InnerElements.Remove(element as Element);
        //}

        //public bool Update(Element element)
        //{
        //    this.LatestUpdateDate = DateTime.Now;
        //    var el = this.InnerElements
        //        .FirstOrDefault(i=>i.Name == element.Name && i.Culture == element.Culture && i.Category == element.Category);

        //    if (el == null)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        el.Name = element.Name;
        //        el.Culture = element.Culture;
        //        el.Category = element.Category;
        //        el.Value = element.Value;

        //        return true;
        //    }
        //}

        //#endregion

        //DateTime LatestFlushDate
        //{
        //    get;
        //    set;
        //}
        //DateTime LatestUpdateDate
        //{
        //    get;
        //    set;
        //}

        //void Flush(object state)
        //{
        //    if ( (bool)state == true ||  this.LatestUpdateDate != this.LatestFlushDate)
        //    {
        //        lock (this.InnerElements)
        //        {
        //            XElement root = new XElement("Elements");
        //            foreach (var el in this.InnerElements)
        //            {
        //                root.Add(new XElement("Element", 
        //                    new XAttribute("Name", el.Name),
        //                    new XAttribute("Culture",el.Culture),
        //                    new XAttribute("Category",el.Category),
        //                    el.Value
        //                ));  
        //            }

        //            XDocument doc = new XDocument(
        //                new XDeclaration("1.0","utf-8","yes"),
        //                root);

        //            var folder = Environments.ApplicationPath + "\\I18N";
        //            if (!System.IO.Directory.Exists(folder))
        //            {
        //                System.IO.Directory.CreateDirectory(folder);
        //            }

        //            using (XmlWriter writer = XmlWriter.Create(this.ElementsFile))
        //            {
        //                doc.WriteTo(writer);
        //            }
                    

        //            this.LatestUpdateDate = this.LatestFlushDate = DateTime.Now;
        //        }
        //    }
        //}

        //public void Refresh(bool isEnforced)
        //{
        //    this.Flush(isEnforced);
        //}

        #region IElementProvider Members

        public IQueryable<IElement> Elements
        {
            get { throw new NotImplementedException(); }
        }

        public bool Add(IElement element)
        {
            throw new NotImplementedException();
        }

        public bool Remove(IElement element)
        {
            throw new NotImplementedException();
        }

        public bool Update(IElement element)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
